package com.szw.dlyk.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szw.dlyk.config.listener.UploadDataListener;
import com.szw.dlyk.constant.Constant;
import com.szw.dlyk.domain.Clue;
import com.szw.dlyk.query.BaseQuery;
import com.szw.dlyk.query.ClueQuery;
import com.szw.dlyk.service.ClueService;
import com.szw.dlyk.mapper.ClueMapper;
import com.szw.dlyk.util.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
* @author asus
* @description 针对表【t_clue(线索表)】的数据库操作Service实现
* @createDate 2024-01-21 18:16:51
*/
@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue>
    implements ClueService{

    @Resource
    private ClueMapper clueMapper;

    @Override
    public PageInfo<Clue> getClueByPage(Integer current) {
        // 1.设置PageHelper
        PageHelper.startPage(current, Constant.PAGE_SIZE);
        // 2.查询
        List<Clue> list = clueMapper.selectClueByPage(BaseQuery.builder().build());
        // 3.封装分页数据到PageInfo
        PageInfo<Clue> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void importExcel(InputStream inputStream, String token) {
        //链式编程，3个参数, 第一个参数是要读取的Excel文件，第二个参数是Excel模板类，第三个参数是文件读取的监听器
        EasyExcel.read(inputStream, Clue.class, new UploadDataListener(clueMapper, token))
                .sheet()
                .doRead();
    }

    @Override
    public Boolean checkPhone(String phone) {
        int count = clueMapper.selectByCount(phone);
        return count <= 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveClue(ClueQuery clueQuery) {
        int count = clueMapper.selectByCount(clueQuery.getPhone());
        if (count <= 0) {
            Clue clue = new Clue();

            //把前端提交过来的参数数据对象ClueQuery复制到TClue对象中
            //Spring框架有个工具类BeanUtils可以进行对象的复制,复制的条件要求是：两个对象的字段名要相同，字段的类型也相同，这样才可以复制
            BeanUtils.copyProperties(clueQuery, clue);

            //解析jwt得到userId
            Integer loginUserId = JwtUtil.parseUserFromJWT(clueQuery.getToken()).getId();

            clue.setCreateTime(new Date()); //创建时间
            clue.setCreateBy(loginUserId); //创建人id

            return clueMapper.insertSelective(clue);
        } else {
            throw new RuntimeException("该手机号已经录入过了，不能再录入");
        }
    }

    @Override
    public Clue getClueById(Integer id) {
        return clueMapper.selectDetailById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateClue(ClueQuery clueQuery) {
        Clue clue = new Clue();

        //把前端提交过来的参数数据对象ClueQuery复制到TClue对象中
        //Spring框架有个工具类BeanUtils可以进行对象的复制,复制的条件要求是：两个对象的字段名要相同，字段的类型也相同，这样才可以复制
        BeanUtils.copyProperties(clueQuery, clue);

        //解析jwt得到userId
        Integer loginUserId = JwtUtil.parseUserFromJWT(clueQuery.getToken()).getId();

        clue.setEditTime(new Date()); //编辑时间
        clue.setEditBy(loginUserId); //编辑人id

        return clueMapper.updateByPrimaryKeySelective(clue);
    }
}




